package com.haohope.framework.datasource;

import static com.alibaba.druid.util.Utils.getBoolean;

import java.sql.SQLException;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.Utils;

public class DataSourceBuilder {
	private final static Log  LOG = LogFactory.getLog(DataSourceBuilder.class);
	public static void configFromPropety(DruidDataSource dataSource,Properties properties) {
		{
            Boolean value = getBoolean(properties, "druid.testWhileIdle");
            if (value != null) {
                //dataSource.testWhileIdle = value;
                dataSource.setTestWhileIdle(value);
            }else {
            	//dataSource.testWhileIdle = true;
            	dataSource.setTestWhileIdle(true);
            }
        }
        {
        	//程序 申请 连接时,进行连接有效性检查（低效，影响性能）
            Boolean value = getBoolean(properties, "druid.testOnBorrow");
            if (value != null) {
                //dataSource.testOnBorrow = value;
                dataSource.setTestOnBorrow(value);
            }else {
            	//dataSource.testOnBorrow = false;
            	dataSource.setTestOnBorrow(false);
            }
        }
        {
        	//程序 返还 连接时,进行连接有效性检查（低效，影响性能）
            Boolean value = getBoolean(properties, "druid.testOnReturn");
            if (value != null) {
                //dataSource.testOnReturn = value;
                dataSource.setTestOnReturn(value);
            }else {
            	//dataSource.testOnReturn = false;
            	dataSource.setTestOnReturn(false);
            }
        }
        {
            String property = properties.getProperty("druid.validationQuery");
            if (property != null && property.length() > 0) {
                dataSource.setValidationQuery(property);
            }else {
            	dataSource.setValidationQuery("SELECT 'x' ");
            }
        }
        {
        	//配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
        	Long timeBetweenEvictionRunsMillis = Utils.getLong(properties,"druid.timeBetweenEvictionRunsMillis");
            if(null==timeBetweenEvictionRunsMillis) {
            	timeBetweenEvictionRunsMillis = 60000l;
            }
            dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        }
        {
            //配置获取连接等待超时的时间
        	Integer maxWait = Utils.getInteger(properties,"druid.maxWait");
        	//60000
        	if(null==maxWait) {
        		maxWait = 60000;
        	}
        	dataSource.setMaxWait(maxWait);
        }
        {
            //池中某个连接的空闲时长达到 N 毫秒后, 连接池在下次检查空闲连接时，将回收该连接,要小于防火墙超时设置
        	Long minEvictableIdleTimeMillis = Utils.getLong(properties, "druid.minEvictableIdleTimeMillis");
        	if(null==minEvictableIdleTimeMillis) {
        		minEvictableIdleTimeMillis = 300000l;
        	}
        	dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        }
        {
            //初始化连接大小
        	Integer initialSize = Utils.getInteger(properties,"druid.initialSize");
        	if(null==initialSize) {
        		initialSize = 10;
        	}
        	dataSource.setInitialSize(initialSize);//
        }
        {
        	//连接池最小空闲
        	Integer minIdle = Utils.getInteger(properties,"druid.minIdle");
        	if(null==minIdle) {
        		minIdle = 10;
        	}
        	dataSource.setMinIdle(minIdle);
        }
        {
        	//连接池最大使用连接数
        	Integer maxActive = Utils.getInteger(properties,"druid.maxActive");
        	if(null==maxActive) {
        		maxActive = 20;
        	}
        	dataSource.setMaxActive(maxActive);
        }
        {
        	//每个连接最多缓存多少个SQL 20
        	Integer maxPoolPreparedStatementPerConnectionSize = Utils.getInteger(properties,"druid.maxPoolPreparedStatementPerConnectionSize");
        	if(null==maxPoolPreparedStatementPerConnectionSize) {
        		maxPoolPreparedStatementPerConnectionSize = 20;
        	}
        	dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        }
        {
        	//这里配置的是插件,常用的插件有: 	stat,wall,slf4j
          	//监控统计: filter:stat 	 
          	//日志监控: filter:log4j 或者 slf4j 	 
          	//防御SQL注入: filter:wall
            String property = properties.getProperty("druid.filters");
            if (property != null && property.length() > 0) {
                try {
                    dataSource.setFilters(property);
                } catch (SQLException e) {
                    LOG.error("setFilters error", e);
                }
            }else {
            	try {
                    dataSource.setFilters("stat,wall,log4j,config");
                } catch (SQLException e) {
                    LOG.error("setFilters error", e);
                }
            }
        }
	}
}
